#include "video.h"
#include "scene.h"

#include <stdio.h>

int cleanup();

int main(int argc, char **argv)
{
    Screen screen = { 800, 600, 24, 0 };
    SDL_Event event;

    int scene_active = 0;
    int frame = 0;
    int frames_per_scene = 5000;
    int benchmark_done = 0;
    float time_start, time_end, time_elapsed;

    printf("======================================\n");
    printf("GLMark\n");
    printf("======================================\n");

    init_video(&screen);

    if (!init_scene())
    {
        fprintf(stderr, "Scene initialize failed, exiting\n");
        cleanup();
        return 1;
    }

    printf("OpenGL Information\n");
    printf("GL_VENDOR:	%s\n", glGetString(GL_VENDOR));
    printf("GL_RENDERER:	%s\n", glGetString(GL_RENDERER));
    printf("GL_VERSION:	%s\n", glGetString(GL_VERSION));
    /*    printf("GL_EXTENSIONS:	%s\n", glGetString(GL_EXTENSIONS)); */
    printf("======================================\n");

    while (!benchmark_done)
    {
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT ||
                    (event.type == SDL_KEYDOWN &&
                     event.key.keysym.sym == SDLK_ESCAPE))
                return cleanup();
        }

        switch (scene_active)
        {
        case 0:
            scene_active = 1;
            printf("No precompilation\n");
            time_start = SDL_GetTicks() / 1000.0f;
            frame = 0;
            break;

        case 1:
            draw_scene(0);
            frame++;
            if (frame == frames_per_scene)
            {
                time_end = SDL_GetTicks() / 1000.0f;
                time_elapsed = time_end - time_start;

                printf("	Duration: %f\n", time_elapsed);
                printf("	FPS: %f\n\n", frames_per_scene / time_elapsed);
                printf("Display Lists\n");

                scene_active = 2;
                frame = 0;
                time_start = SDL_GetTicks() / 1000.0f;
            }
            break;

        case 2:
            draw_scene(1);
            frame++;
            if (frame == frames_per_scene)
            {
                time_end = SDL_GetTicks() / 1000.0f;
                time_elapsed = time_end - time_start;

                printf("	Duration: %f\n", time_elapsed);
                printf("	FPS: %f\n\n", frames_per_scene / time_elapsed);
                printf("Vertex Arrays\n");

                scene_active = 3;
                frame = 0;
                time_start = SDL_GetTicks() / 1000.0f;
            }
            break;

        case 3:
            draw_scene(2);
            frame++;
            if (frame == frames_per_scene)
            {
                time_end = SDL_GetTicks() / 1000.0f;
                time_elapsed = time_end - time_start;

                printf("	Duration: %f\n", time_elapsed);
                printf("	FPS: %f\n\n", frames_per_scene / time_elapsed);
                printf("Nearest Filtered Texture\n");

                scene_active = 4;
                frame = 0;
                time_start = SDL_GetTicks() / 1000.0f;
            }
            break;

        case 4:
            draw_scene(3);
            frame++;
            if (frame == frames_per_scene)
            {
                time_end = SDL_GetTicks() / 1000.0f;
                time_elapsed = time_end - time_start;

                printf("	Duration: %f\n", time_elapsed);
                printf("	FPS: %f\n\n", frames_per_scene / time_elapsed);
                printf("Linear Filtered Texture\n");

                scene_active = 5;
                frame = 0;
                time_start = SDL_GetTicks() / 1000.0f;
            }
            break;
        case 5:
            draw_scene(4);
            frame++;
            if (frame == frames_per_scene)
            {
                time_end = SDL_GetTicks() / 1000.0f;
                time_elapsed = time_end - time_start;

                printf("	Duration: %f\n", time_elapsed);
                printf("	FPS: %f\n\n", frames_per_scene / time_elapsed);
                printf("MipMapped Texture\n");

                scene_active = 6;
                frame = 0;
                time_start = SDL_GetTicks() / 1000.0f;
            }
            break;
        case 6:
            draw_scene(5);
            frame++;
            if (frame == frames_per_scene)
            {
                time_end = SDL_GetTicks() / 1000.0f;
                time_elapsed = time_end - time_start;

                printf("	Duration: %f\n", time_elapsed);
                printf("	FPS: %f\n\n", frames_per_scene / time_elapsed);

                benchmark_done = 1;
            }
            break;
        }

        SDL_GL_SwapBuffers();
    }

    cleanup();

    return 0;
}

int cleanup()
{
    /*    printf("cleanup()\n"); */
    deinit_video();
    deinit_scene();
    exit(EXIT_SUCCESS);

    return 0;
}
